package com.frejoys.dao.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.frejoys.common.vo.agent.AgentUserInfoVo;
import com.frejoys.common.vo.store.StoreUserVo;
import com.frejoys.dao.entity.StoreUser;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

public interface StoreUserMapper extends BaseMapper<StoreUser> {

    // 获取用户关联店铺id
    @Select("select id from j_store_user where user_id = #{userId} and store_id=#{storeId} and role=#{role}")
    Integer getIdByStoreRole(@Param("userId") Long userId, @Param("storeId") Integer storeId, @Param("role") Integer role);

    /**
     * 查询商家用户拥有的店铺id
     */
    @Select("select store_id from j_store_user where user_id = #{userId} ")
    List<Integer> searchStoreIds(@Param("userId") Long userId);

    /**
     * 根据店铺ids查询店铺拥有者
     */
    List<StoreUserVo> getStoreUserByStoreIds(@Param("storeIds") List<Integer> storeIds);

    /**
     * 根据店铺id查询店铺拥有者
     */
    @Select("SELECT su.id storeId,u.id userId,u.nickname,u.country_code,u.phone FROM j_store_user su left join j_user u on su.user_id = u.id where  " +
            " su.role = 1 and u.status = 1 and su.id  = #{storeId} ")
    StoreUserVo getStoreUserByStoreId(@Param("storeId") Integer storeId);


    /**
     * 根据店铺id查询店铺代理
     */
    AgentUserInfoVo getStoreAgentUserByStoreId(@Param("storeId") Integer storeId);


    // 根据用户id和店铺id获取用户关联店铺id
    @Select("select id from j_store_user where user_id = #{userId} and store_id=#{storeId}")
    Integer getStoreUserId(@Param("userId") Long userId, @Param("storeId") Integer storeId);
}
